<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>姚嘉熙的blog</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
            color: #333;
        }
        .container {
            width: 80%;
            margin: auto;
            overflow: hidden;
        }
        .post {
            background: #fff;
            border: 1px solid #ddd;
            padding: 20px;
            margin-bottom: 20px;
        }
        .post h2 {
            margin-top: 0;
        }
        .post p {
            font-size: 16px;
            color: #666;
        }
        .read-more {
            cursor: pointer;
            color: #0077cc;
        }
        .read-more:hover {
            text-decoration: underline;
        }
        /* 添加弹窗的样式 */
        .modal {
            display: none;
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
        }
        .modal-content {
            background: #fff;
            margin: 10% auto;
            padding: 20px;
            width: 80%;
            max-width: 300px;
        }
        .search-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .search-bar {
            flex-grow: 1;
        }
        .search-bar input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-right: 10px;
        }
        .search-bar input[type="submit"] {
            padding: 10px 20px;
            background: #0077cc;
            color: white;
            border: none;
            cursor: pointer;
            text-decoration: none;
            border-radius: 5px;
        }
        .search-bar input[type="submit"]:hover {
            background: #005fa3;
        }
        .new-post-button {
            text-decoration: underline;
            color: #0077cc;
            background: none;
            border: none;
            padding: 0;
            margin: 0;
            cursor: pointer;
        }
        .new-post-button:hover {
            text-decoration: underline;
            background: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>姚嘉熙的blog</h1>
        <div class="search-container">
            <form class="search-bar" method="get" action="{% url 'post_search' %}">
                <input type="text" name="q" placeholder="搜索文章...">
                <input type="submit" value="搜索">
            </form>
        </div>
        {% if posts %}
            {% for post in posts %}
                <div class="post" data-pk="{{ post.pk }}" data-full-text="{{ post.body }}">
                    <h2>{{ post.title }}</h2>
                    <p>{{ post.body|truncatechars:100 }}</p>
                    <a href="{% url 'post_new' %}" class="new-post-button">新建文章</a>
                    <a href="{% url 'post_detail' post.pk %}" class="read-more">阅读更多</a>
                    <a href="{% url 'post_edit' post.pk %}">修改文章</a>
                    <a href="{% url 'post_delete' post.pk %}" onclick="return confirmDelete()">删除</a>
                </div>
            {% endfor %}
        {% else %}
            <script>
                function showNoPostsModal() {
                    var modal = document.getElementById('noPostsModal');
                    modal.style.display = 'block';
                }
                function closeNoPostsModal() {
                    var modal = document.getElementById('noPostsModal');
                    modal.style.display = 'none';
                }
            </script>
            <div id="noPostsModal" class="modal">
                <div class="modal-content">
                    <p>还没有文章噢。</p>
                    <p>点击这里去<a href="{% url 'post_new' %}" onclick="closeNoPostsModal()">创建文章</a></p>
                    <button onclick="closeNoPostsModal()">关闭</button>
                </div>
            </div>
            <script>
                showNoPostsModal();
            </script>
        {% endif %}
    </div>

    <script>
        function showFullText(pk) {
            var post = document.querySelector('.post[data-pk="' + pk + '"]');
            if (post) {
                var fullText = post.getAttribute('data-full-text');
                post.querySelector('p').innerHTML = fullText; <!-- 设置 innerHTML 来保留 HTML 格式 -->
                post.querySelector('.read-more').style.display = 'none';
            }
        }
    </script>
    <script>
        function confirmDelete() {
            return confirm("确定要删除这篇文章吗？");
        }
    </script>
</body>
</html>